/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BookPanel.java
 *
 * Created on Mar 30, 2011, 11:16:19 PM
 */

package org.libarae.client;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import org.libarae.client.MainScreen.Tabs;
import org.libarae.client.threads.PictureThread;
import org.libarae.client.threads.HasPictureThread;
import org.libarae.communication.common.Book;
import org.libarae.communication.common.Book.BookStatus;

/**
 *
 * @author Ben
 */
@SuppressWarnings("serial")
public class BookPanel extends javax.swing.JPanel implements HasPictureThread  {

    private Book mBook;
    private MainScreen mMainScreen;

    private final int SCALED_IMAGE_WIDTH = 60;
    private final int SCALED_IMAGE_HEIGHT = 60;

    /** Creates new form BookPanel */
    public BookPanel() {
        this.mBook = new Book();
        mBook.setTitle("Title");
        mBook.setAuthor("Author");
        mBook.setISBN("9780590353403");
        mBook.setBookStatus(BookStatus.AVAILABLE);
        
        Image img = (new ImageIcon(getClass().getResource("/res/book.png"))).getImage();

        BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = bimg.createGraphics();
        g.drawImage(img,0,0,null);
        g.dispose();
        mBook.setPicture(bimg);

        initComponents();
    }

    public BookPanel(Book mBook, MainScreen mMainScreen)
    {
        this.mBook = mBook;
        this.mMainScreen = mMainScreen;

        if(mBook.getPicture() == null)
        {
            PictureThread t = new PictureThread(mBook, this);
            t.start();
        }

        initComponents();
    }

    public void setBook(Book mBook)
    {
        this.mBook = mBook;

        revalidate();
    }

    public Book getBook()
    {
        return mBook;
    }

    private Image scaleImage(BufferedImage i)
    {
        if(i == null)
            return null;
        //Scale the image down;
        int width;
        int height;

        if(i.getWidth() > i.getHeight())
        {
            width = SCALED_IMAGE_WIDTH;
            height = (int)(SCALED_IMAGE_HEIGHT * (((float)i.getHeight())/i.getWidth()));
        }
        else
        {
            height = SCALED_IMAGE_HEIGHT;
            width = (int)(SCALED_IMAGE_WIDTH * (((float)i.getWidth())/i.getHeight()));
        }
        
        return i.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
    }

    public void displayPicture(BufferedImage i)
    {
        try {
            SwingUtilities.invokeAndWait(
                    new Runnable() {
                            @Override
                            public void run() {
                                lblCover.setIcon(new ImageIcon(scaleImage(mBook.getPicture())));
                                //throw new UnsupportedOperationException("Not supported yet.");
                            }
                        }
                    );
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCover = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblAuthor = new javax.swing.JLabel();
        lblISBN = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        btnShowInfo = new javax.swing.JButton();
        sepBottom = new javax.swing.JSeparator();

        setBackground(new java.awt.Color(255, 255, 255));
        setMaximumSize(new java.awt.Dimension(32000, 80));
        setMinimumSize(new java.awt.Dimension(0, 80));
        setPreferredSize(new java.awt.Dimension(305, 80));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        lblCover.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lblTitle.setText(mBook.getTitle());

        lblAuthor.setText("By " + mBook.getAuthor());

        lblISBN.setText("ISBN: " + mBook.getISBN());

        lblStatus.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblStatus.setText("Status: " + mBook.getBookStatus().toString());
        lblStatus.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        btnShowInfo.setText("Show Info");
        btnShowInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(lblCover, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTitle)
                            .addComponent(lblAuthor))
                        .addGap(18, 18, 18)
                        .addComponent(lblISBN)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblStatus)
                            .addComponent(btnShowInfo)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(sepBottom, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(lblCover, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(lblTitle)
                        .addGap(6, 6, 6)
                        .addComponent(lblAuthor))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(lblISBN))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(lblStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnShowInfo)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(sepBottom, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        if(mBook.getPicture() != null)
        lblCover.setIcon(new ImageIcon(scaleImage(mBook.getPicture())));
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        ((BookListPanel)this.getParent()).setSelectedItem(this);
    }//GEN-LAST:event_formMousePressed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        if(evt.getClickCount() > 1)
        {
            mMainScreen.getBookInfoPanel().setBook(mBook);
            mMainScreen.switchToTab(Tabs.BOOK_INFO);
        }
    }//GEN-LAST:event_formMouseClicked

    private void btnShowInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowInfoActionPerformed
        mMainScreen.getBookInfoPanel().setBook(mBook);
        mMainScreen.switchToTab(Tabs.BOOK_INFO);
    }//GEN-LAST:event_btnShowInfoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnShowInfo;
    private javax.swing.JLabel lblAuthor;
    private javax.swing.JLabel lblCover;
    private javax.swing.JLabel lblISBN;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JSeparator sepBottom;
    // End of variables declaration//GEN-END:variables

}
